package com.musicsonar.android.utils;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.AsyncTask;
import android.widget.ImageView;


public class WebImage extends AsyncTask<String, Void, String> {

	private ImageView v;
	private String value;
	private Bitmap image;
	private String type;
	private boolean saveImage;
	public static final String LIST = "LIST";
	public static final String SCREEN = "SCREEN";

	public WebImage(ImageView v, String value, String type, boolean saveImage){
		this.v = v;
		this.value = value;
		this.type = type;
		this.saveImage = saveImage;
	}

	protected void onPostExecute(String result) {
		if(v.getDrawable() != null){
			v.getDrawable().setCallback(null);
		}
		v.setImageBitmap(image);
	}

	protected String doInBackground(String... params) {
		image = MusicSonarUtils.loadBitmap(value, saveImage);
		int width = image.getWidth();
		int height = image.getHeight();
		float scaleWidth = 0;
		float scaleHeight = 0;
		if(LIST.equals(type)){
			scaleWidth = ((float) 120) / width;
			scaleHeight = ((float) 120) / height;
		}else{
			scaleWidth = ((float) 300) / width;
			scaleHeight = ((float) 300) / height;
		}
		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		image = Bitmap.createBitmap(image, 0, 0, width, height, matrix, false);
		return "";
	}
}